package edu.stu.mall.adapter

import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import edu.stu.mall.MyApplication.Companion.context
import edu.stu.mall.databinding.ItemClassificationLayoutBinding
import edu.stu.mall.databinding.ItemProductCategoryChildrenBinding
import edu.stu.mall.repository.data.Children
import edu.stu.mall.repository.data.ItemModel
import edu.stu.mall.repository.data.ProductCategoryListDataItem
import edu.stu.mall.service.loadImage

class ProductCategoryChildrenAdapter :
    RecyclerView.Adapter<ProductCategoryChildrenAdapter.ProductCategoryChildrenViewHolder>() {

    private var dataList: List<ProductCategoryListDataItem> = mutableListOf()
    private val productCategoryChildrenListAdapter = ProductCategoryChildrenListAdapter()


    fun setData(list: List<ProductCategoryListDataItem>?) {
        if (list != null && list.isNotEmpty()) {
            dataList = list
            notifyDataSetChanged()
        }
    }


    class ProductCategoryChildrenViewHolder(binding: ItemProductCategoryChildrenBinding) :
        RecyclerView.ViewHolder(binding.root) {
        var itemBinding: ItemProductCategoryChildrenBinding

        init {
            itemBinding = binding
        }
    }

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): ProductCategoryChildrenViewHolder {

        return ProductCategoryChildrenViewHolder(
            ItemProductCategoryChildrenBinding.inflate(
                LayoutInflater.from(parent.context),
                parent, false
            )
        )
    }

    override fun getItemCount(): Int {
        return dataList.size

    }

    override fun onBindViewHolder(holder: ProductCategoryChildrenViewHolder, position: Int) {
//        Glide.with(context).load(dataList[position].imageUrl).into(holder.itemBinding.imageView)
        holder.itemBinding.item = dataList[position]
        val productCategoryListDataItem = dataList[position].children
        holder.itemBinding.rlvChildren.layoutManager = GridLayoutManager(holder.itemView.context, 3)
        holder.itemBinding.rlvChildren.adapter = productCategoryChildrenListAdapter.apply {
            Log.e("TAG", "ssssssss: $productCategoryListDataItem", )
            setChildrenListData(productCategoryListDataItem)
        }

    }
}